﻿#include "stdafx.h"
#include "NetLib.h"
#include "NetManager.h"
#include "PhysXObj.h"
#include "Car.h"
#include "Input.h"
#include "debugassert.h"

_NAMESPACE_BEGIN

NetManager::NetManager() //: 
		//client(NULL),server(NULL), isConnected(false)
{
	netClock.Start();
}

NetManager::~NetManager()
{

	isWaitingRoom = false;

	if(net)
	{
		net->Disconnect();
		net->ReleaseInterface();
	}

	SAVE_LOG("网络引擎释放完毕");
}

void NetManager::Shutdown()
{
	net->Disconnect();
}

void NetManager::Release()
{
	net->ReleaseInterface();
}

void NetManager::Update()
{
	// 双方FPS大于60时可保证网络更新同步
	if (netClock.GetElapse() <0.0167f)
	{
		return ;
	}
		
	netClock.Start();

	if (net && net->selfCar)
	{
			// 客户端更新
		//	if (!net->isServer)
		//	{
				//CarPoseData data;
				//data.PacketId = ID_CAR_POSE;
				//data.netID = net->netID;
				////data.pose = net->selfCar->GetPose();
				//data.pose = net->selfCar->GetActor()->getShapes()[0]->getGlobalPose();

				NodeData data;
				data.PacketId = ID_CAR_POSE;
				data.netID = net->netID;
				data.quat = net->selfCar->GetCarNode()->getOrientation();
				data.pos = net->selfCar->GetCarNode()->getPosition();

				net->netIntface->Send
					((char*)&data, sizeof(NodeData), 
					HIGH_PRIORITY, RELIABLE_ORDERED, 0,
					UNASSIGNED_SYSTEM_ADDRESS , true);

				//ControlData	data;
				//data.PacketId = ID_CAR_POSE;
				//data.left = InputCenter::Instance()->GetKeyboard()->isKeyDown(OIS::KC_A);
				//data.right = InputCenter::Instance()->GetKeyboard()->isKeyDown(OIS::KC_D);
				//data.forward = InputCenter::Instance()->GetKeyboard()->isKeyDown(OIS::KC_W);
				//data.backward = InputCenter::Instance()->GetKeyboard()->isKeyDown(OIS::KC_S);
				//data.handbrake = InputCenter::Instance()->GetKeyboard()->isKeyDown(OIS::KC_SPACE);
				//data.excursion = InputCenter::Instance()->GetKeyboard()->isKeyDown(OIS::KC_RSHIFT) && (data.left||data.right);
				//data.thruster = InputCenter::Instance()->GetKeyboard()->isKeyDown(OIS::KC_Q);

				//net->netIntface->Send
				//	((char*)&data, sizeof(ControlData), 
				//	HIGH_PRIORITY, RELIABLE_ORDERED, 0,
				//	static_cast<NetClient*>(net)->serverAdd , false);
		//	}	
	}

	if (isWaitingRoom)
	{
		UpdateWaitingRoom();
	}
}

void NetManager::CreateAsServer()
{
	if(net)
	{
		delete net;
	}
	net = new NetServer();
	
	SetWaitingRoomFlag(true);
}

void NetManager::CreateAsClient()
{
	if(net)
	{
		delete net;
	}
	net = new NetClient();

	SetWaitingRoomFlag(true);
}

void NetManager::UpdateWaitingRoom()
{
	net->Receive();
}

void NetManager::SeekforServer( int time )
{
	roomList.clear();
	std::string IPString("");
	
	// 组播
	net->netIntface->Ping("255.255.255.255", SERVER_PORT, true);
	
	SAVE_LOG("组播消息，探测开启的服务器...");

	// 等待服务器响应
	RakNetTime quitTime = RakNet::GetTime() + time;

	while (RakNet::GetTime() < quitTime)
	{
		Packet *packet = net->Receive();

		// 未接收到数据包时等待
		if (packet==0)
		{
			Sleep(10);
			continue;
		}
		else
		{
			// 获得服务器列表
			if (packet->data[0]==ID_PONG)
			{
				IPString = packet->systemAddress.ToString(false);

				if( IPString != "" )
				{
					RoomDesc tempRoomInfo;
					tempRoomInfo.hostName = "Unname";
					tempRoomInfo.passWord = "";
					tempRoomInfo.serverAdd = packet->systemAddress;
					roomList.push_back(tempRoomInfo);

					string temp = "找到开启的服务器： ";
					temp += IPString;
					SAVE_LOG(temp);
				}
			}
		}
		Sleep(10);
	}

	if (roomList.empty())
	{
		SAVE_LOG("未能在局域网中探测到开启的服务器");
	}
}

void NetManager::GetRoomList( UINT id, RoomDesc& out )
{
	out.hostName = roomList[id].hostName;
	out.serverAdd = roomList[id].serverAdd;
	out.passWord = roomList[id].passWord;
}

UINT NetManager::GetServerCount()
{
	return roomList.size();
}

UINT NetManager::GetNetID()
{
	return net->netID;
}

void NetManager::GetSelfCar( PhysCar* car )
{
	assert(car);
	net->selfCar = car;
}

_NAMESPACE_END

